
package Figuras;

import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.Vector;

public abstract class Figura implements  Serializable {

    protected Integer x, y, w, h;
    protected Color color;
    transient protected Integer vida=10;
    transient protected Integer dx=0;
    transient protected Integer dy=0;
    protected Lienzo lienzo;
    protected Integer orientacion=0;
    transient protected Integer tiempoParaProximoDisparo=0;
    transient protected boolean disparar=false;
    transient protected String nombre;

    protected Figura(Integer x0, Integer y0, Integer w0, Integer h0, Color c, 
            Lienzo l) {
        x=x0;
        y=y0;
        w=w0;
        h=h0;
        color=c;
        lienzo=l;
    }
    transient Vector<Figura> vTemp=new Vector<Figura>();
    transient public int i;
    public void ira(Integer x0, Integer y0){
        x = x0;
        y = y0;
        vTemp.clear();
        for(int j=0; j<lienzo.getF().size();j++)
            vTemp.add(lienzo.getF().get(j));
        for(int j=0; j<lienzo.getF2().size();j++)
            vTemp.add(lienzo.getF2().get(j));
        for(i=0; i<vTemp.size();i++){
            if(Boolean.valueOf(this.rect().intersects(vTemp.get(i).rect())))
                if(!Boolean.valueOf(this.equals(vTemp.get(i))))
                    this.colisionoCon(vTemp.get(i));
        }
    }

    public abstract void colisionoCon(Bala c);
    public abstract void colisionoCon(CirculoBomba c);
    public abstract void colisionoCon(CirculoVida c);
    public abstract void colisionoCon(Cuadrilatero c);
    public abstract void colisionoCon(Figura c);
    public abstract void colisionoCon(Pared c);
    public abstract void colisionoCon(Movil c);
    public abstract void colisionoCon(ParedH c);
    public abstract void colisionoCon(ParedV c);

    public abstract void dibujate(Graphics g);

    protected Rectangle rect(){
        Rectangle r=  new Rectangle( x, y, w, h);
        return r;
    }

    public void movete(){
           this.ira(x+dx,y+dy);
           if(disparar && tiempoParaProximoDisparo==0 ){
               tiempoParaProximoDisparo=10;
               switch(orientacion){
                   case 0:
                       Figura ff=new Bala(x+w+2,y-2+h/2,5,5, Color.WHITE, lienzo);
                       ff.setDx(10);
                       lienzo.getF2().add(ff);
                       break;
                   case 1:
                       Figura f1=new Bala(x-2+w/2,y+h+2,5,5, Color.WHITE, lienzo);
                       f1.setDy(10);
                       lienzo.getF2().add(f1);
                       break;
                   case 2:
                       Figura f2=new Bala(x-6,y+8,5,5, Color.WHITE, lienzo);
                       f2.setDx(-10);
                       lienzo.getF2().add(f2);
                       break;
                   case 3:
                       Figura f3=new Bala(x-2+w/2,y-6,5,5, Color.WHITE, lienzo);
                       f3.setDy(-10);
                       lienzo.getF2().add(f3);
                       break;
                   default:
                       break;
               }
           }
           else{
               if(tiempoParaProximoDisparo>0)
                   tiempoParaProximoDisparo--;
           }
    }

    public int puedeDisparar(){
        return (int)(tiempoParaProximoDisparo);
    }
    public void setDisparar(boolean disp){
        disparar=disp;
    }
    protected void pierdeVida(int i) {
            vida=vida+i;
            if(Boolean.valueOf(vida.equals(Integer.valueOf(0)))){
                this.lienzo.getF2().remove(this);
            }else{
                if(Boolean.valueOf(vida.equals(Integer.valueOf(11))))
            vida=10;
            }
    }
    @Override
    public String toString(){
        return "["+x+" "+y+" "+w+" "+h+"]";
    }

    public Integer getDx() {
        return dx;
    }
    public Integer getDy() {
        return dy;
    }
    public void setDx(Integer dx) {
        this.dx = dx;
    }
    public void setDy(Integer dy) {
        this.dy = dy;
    }
    public void setDxO(Integer dx, Integer or) {
        this.dx = dx;
        orientacion=or;
    }
    public void setDyO(Integer dy, Integer or) {
        this.dy = dy;
        orientacion=or;
    }
    public void setX(Integer x0){
        x=x0;
    }
    public void setY(Integer y0){
        y=y0;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
    public void setColor(Color col){
        color=col;
    }
     public void setOrientacion(Integer or){
        orientacion=or;
    }
    public int getOrientacion(){
        return orientacion;
    }
    public void setNombre(String name){
        nombre=name;
    }
    public String getNombre(){
        return nombre;
    }
    public int getvida(){
        return vida;
    }
    public Color getColor(){
        return color;
    }
    public Lienzo getLienzo(){
        return lienzo;
    }
}
